package com.lxx.wiki.handler;


import com.alibaba.fastjson2.JSON;
import com.lxx.wiki.domain.resp.CommonResp;
import com.lxx.wiki.utils.WebUtils;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class AuthenticationEntryPointImpl implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
//        401错误通常与认证（登录）相关
//        而403错误与权限（访问控制）相关
        CommonResp result = new CommonResp();
        result.setCode(HttpStatus.UNAUTHORIZED.value());
        result.setMessage("认证失败请重新登录");
        String json = JSON.toJSONString(result);
        WebUtils.renderString(response, json);

    }
}

